package com.example.medicinetime;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

// <https://www.jianshu.com/p/a84ddaf530ec>
//https://cloud.tencent.com/developer/article/1729227
public class AlarmReceiver extends BroadcastReceiver {
    private NotificationManager m_notificationMgr = null;
    private static final int NOTIFICATION_FLAG = 1000;
    private static final String TAG="AlarmReceiver";
    public static MediaPlayer mediaPlayer;
    @RequiresApi(api = Build.VERSION_CODES.N)
    @SuppressLint("NewApi")

    @Override
    public void onReceive(Context context, Intent intent) {
        m_notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);//必须加
        Log.i(TAG, "onReceive: ");
        if (intent.getAction().equals("VIDEO_TIMER")) {
            //播放音乐
            mediaPlayer = new MediaPlayer();
            mediaPlayer = MediaPlayer.create(context, R.raw.dylanf);
            mediaPlayer.start();
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                    new Intent(context, MainActivity.class), 0);
            // 通过Notification.Builder来创建通知，API16之后才支持
            Notification notify = new Notification.Builder(context)
                    .setSmallIcon(R.drawable.notification)
                    .setTicker(context.getResources().getString(R.string.n_tick))
                    .setContentTitle(context.getResources().getString(R.string.n_title))
                    .setContentText(context.getResources().getString(R.string.n_detail))
                    .setContentIntent(pendingIntent).setNumber(1).build(); // API11可以使用getNotificatin()来替代

            notify.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时，通知将被清除。

            NotificationManager manager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(NOTIFICATION_FLAG, notify);// 通过通知管理器来发起通知。如果id不同，则每click，在status哪里增加一个提示
        }
    }
}
